package com.ts.api.module.api.convert.market.context;

import com.gitee.apanlh.util.base.StringUtils;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.constant.api.ApiProviderEnum;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.convert.core.annotation.ApiConsumerMarketConvertType;
import com.ts.api.module.api.convert.market.convert.ConsumerMarketConvert;
import com.ts.api.module.api.convert.market.handler.ConsumerMarketConvertHandler;
import com.ts.api.module.channel.entity.po.ChannelMarketPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  API下游贷超-转换上下文
 *
 *  @author Pan
 */
@Component
public class ApiConsumerMarketConvertContext {

    @Autowired
    private ConsumerMarketConvert consumerMarketConvert;

    /**
     *  获取下游贷超撞库匹配器
     *
     *  @param  channelMarketPO                     渠道贷超实体
     *  @return ConsumerMarketConvertHandler
     */
    public ConsumerMarketConvertHandler getConsumerDataExchangeHandler(ChannelMarketPO channelMarketPO) {
        Integer apiProviderType = channelMarketPO.getApiProviderType();
        if (apiProviderType == null) {
            throw new ApiMsgException(StringUtils.format("下游贷超撞库-贷超id:{}, 无接入方无须接入", channelMarketPO.getId()));
        }

        //  下游-贷超撞库类型
        ApiConsumerMarketConvertType apiConsumerMarketConvertType = ApiConsumerMarketConvertType.getDataExchangeEnum(channelMarketPO.getApiDataExchangeType());

        //  我方接入
        if (ApiProviderEnum.hasWeProvider(channelMarketPO.getApiProviderType())) {
            Long marketId = channelMarketPO.getId();
            ConsumerMarketConvertHandler handler = consumerMarketConvert.getHandler(marketId, apiConsumerMarketConvertType);
            if (handler == null) {
                throw new ApiMsgException(ApiEnum.API_NULL_FAIL.format("下游贷超撞库-无对应-解析器", "null"));
            }
            return handler;
        }
        //  对方接入
        return consumerMarketConvert.getDefaultHandler(apiConsumerMarketConvertType);
    }

    /**
     *  获取下游贷超联登匹配器
     *
     *  @param  channelMarketPO                 渠道贷超实体
     *  @return ConsumerMarketConvertHandler
     */
    public ConsumerMarketConvertHandler getConsumerFederateHandler(ChannelMarketPO channelMarketPO) {
        Integer apiProviderType = channelMarketPO.getApiProviderType();
        if (apiProviderType == null) {
            throw new ApiMsgException(StringUtils.format("下游贷超联登-贷超id:{}, 无接入方无须接入", channelMarketPO.getId()));
        }

        //  下游-贷超联登类型
        ApiConsumerMarketConvertType apiConsumerMarketConvertType = ApiConsumerMarketConvertType.getFederateEnum(channelMarketPO.getApiFederateType());

        //  我方接入
        if (ApiProviderEnum.hasWeProvider(channelMarketPO.getApiProviderType())) {
            Long marketId = channelMarketPO.getId();
            ConsumerMarketConvertHandler handler = consumerMarketConvert.getHandler(marketId, apiConsumerMarketConvertType);
            if (handler == null) {
                throw new ApiMsgException(ApiEnum.API_NULL_FAIL.format("下游贷超联登-无对应-解析器", "null"));
            }
            return handler;
        }
        //  对方接入
        return consumerMarketConvert.getDefaultHandler(apiConsumerMarketConvertType);
    }
}
